package com.itheima.config;

import com.itheima.auth.filter.JwtAuthenticationTokenFilter;
import com.itheima.auth.handle.MyAccessDeniedHandler;
import com.itheima.auth.handle.MyAuthenticationFailureHandler;
import com.itheima.auth.handle.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * @author jshstart
 * @creat 2020-11-21 14:31
 */
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private MyAccessDeniedHandler myAccessDeniedHandler;
    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;
    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        //swagger-ui放行
        web.ignoring().antMatchers("/v2/api-docs",
                "/webjars/**",
                "/swagger-resources/configuration/ui",
                "/swagger-resources",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html",
                "operatingReport/downLoad"
        );
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
         http.formLogin()
          //登陆的验证的处理
                 .successHandler(myAuthenticationSuccessHandler)//成功处理
                 .failureHandler(myAuthenticationFailureHandler)//失败的处理
        .and()
                 .exceptionHandling().accessDeniedHandler(myAccessDeniedHandler)
        .and()
                 .authorizeRequests()
                 .antMatchers("/*login*","/operatingReport/downLoad").permitAll()//跳转登陆页面时放行
                 .anyRequest().authenticated()//剩余的进行拦截
        .and()
                 .csrf().disable()
                 .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
                 .addFilterBefore(jwtAuthenticationTokenFilter,UsernamePasswordAuthenticationFilter.class);

    }
}
